package com.dianrui.fmi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.Optional;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditStockAdapter extends BaseAdapter {
    private Context mContext;

    private List<Optional> mDatas;

    private LayoutInflater mInflater;

    public boolean flage = false;

    private boolean isShow = false;

    private EditStockClickListener editStockClickListener;


    public EditStockAdapter(Context mContext, List<Optional> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(this.mContext);

    }

    public void refresh(List<Optional> list) {
        this.mDatas = list;
        this.notifyDataSetChanged();
    }

    public interface EditStockClickListener {

        void onTop(int pos);

        void getName(int pos,String names);

    }

    public void setOnButtonClickListener(EditStockClickListener editStockClickListener) {
        this.editStockClickListener = editStockClickListener;
    }

    public boolean isShow() {
        return isShow;
    }

    public void setShow(boolean isShow) {
        this.isShow = isShow;
    }

    @Override
    public int getCount() {
        if (mDatas.size() == 0) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView =LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_stock, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        if (mDatas != null && mDatas.size()>0) {
            final Optional dataBean = mDatas.get(position);
            if (dataBean != null) {
                holder.stocknames.setText(dataBean.full_symbol);


                holder.clauseChoose.setChecked(dataBean.isCheck);


                holder.clauseChoose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dataBean.isCheck) {
                            dataBean.isCheck = false;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position, dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(Color.WHITE);
                        } else {
                            dataBean.isCheck = true;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position, dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(parent.getContext().getResources().getColor(R.color.transparent_ffe9f0fa));
                        }
                    }
                });

                if (isShow) {
                    holder.clauseChoose.setVisibility(View.VISIBLE);
                }else {
                    holder.clauseChoose.setVisibility(View.GONE);
                }

                holder.editStockLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dataBean.isCheck) {
                            dataBean.isCheck = false;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position, dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(Color.WHITE);
                        } else {
                            dataBean.isCheck = true;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position, dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(parent.getContext().getResources().getColor(R.color.transparent_ffe9f0fa));
                        }
                    }
                });

                holder.stocknames.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dataBean.isCheck) {
                            dataBean.isCheck = false;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position,dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(Color.WHITE);

                        } else {
                            dataBean.isCheck = true;
                            if (editStockClickListener != null) {
                                editStockClickListener.getName(position, dataBean.full_symbol);
                            }
                            holder.maineditStockLayout.setBackgroundColor(parent.getContext().getResources().getColor(R.color.transparent_ffe9f0fa));
                        }
                    }
                });
                holder.topLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        editStockClickListener.onTop(position);
                    }
                });


            }
        }



        return convertView;
    }




    class ViewHolder {
        @BindView(R.id.clause_choose)
        CheckBox clauseChoose;
        @BindView(R.id.stocknames)
        TextView stocknames;
        @BindView(R.id.top_layout)
        LinearLayout topLayout;
        @BindView(R.id.drag_layout)
        LinearLayout dragLayout;
        @BindView(R.id.edit_stock_layout)
        LinearLayout editStockLayout;
        @BindView(R.id.main_edit_stock_layout)
        LinearLayout maineditStockLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }



    //交换位置
    public void change(int start, int end) {
        Optional optional = mDatas.get(start);
        mDatas.remove(optional);
        mDatas.add(end, optional);
        notifyDataSetChanged();
    }
}
